package testcase.app;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;


import api.app.CloseSubject;
import api.app.StartChat;
import api.doctor.UpdateStatus;
import common.Config;
import common.Host;
import common.TestData;
/**
 * 帮忙医APP开启有报告会话接口StartChat测试用例
 * @author gaowen
 *
 */
public class TestStartChat
{
	/**
	 * 帮忙医APP开启有报告会话接口StartChat接口数据驱动
	 * @return 测试数据
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@DataProvider(name="Test_HasReport")
	public static String[][] testStartChatDataProvider_HasReport() throws IOException, ParserConfigurationException, SAXException
	{
		Host.setup("Test");
		TestData td=new TestData("app/TestStartChat.xml");
		return td.getParam("HasReport");
	}
	
	/**
	 * 帮忙医APP开启无报告会话接口StartChat接口数据驱动
	 * @return 测试数据
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	@DataProvider(name="Test_NoReport")
	public static String[][] testStartChatDataProvider_NoReport() throws ParserConfigurationException, SAXException, IOException
	{
		Host.setup("Test");
		TestData td=new TestData("app/TestStartChat.xml");
		return td.getParam("NoReport");
	}
	/**
	 * 测试前上线医生
	 * @throws IOException
	 */
	@BeforeClass(groups="Test")
	public void testStartChatBeforeTest() throws IOException
	{
		Host.setup("Test");
		String doctorID=Config.getValue("Doctor-Test", "ID");
		String doctorToken=Config.getValue("Doctor-Test", "token");
		UpdateStatus.put("true", "1", doctorID, doctorToken);
	}
	
	
	/**
	 * 帮忙医APP开启有报告会话接口StartChat接口测试用例
	 * @param caseName 用例名
	 * @param userID 用户UID
	 * @param userName 用户姓名
	 * @param userSex 用户性别
	 * @param age 用户年龄
	 * @param studyID 用户StudyID
	 * @param token 用户token
	 * @throws NumberFormatException
	 * @throws JSONException
	 * @throws IOException
	 */
	@Test(dataProvider="Test_HasReport",groups="Test")
	public void testStartChatTestCase_HasReport(String caseName,String userID,String userName,String userSex,String age,String studyID,String token) throws NumberFormatException, JSONException, IOException
	{
		//调用接口并将返回值转成Json格式
		String result=StartChat.post(Integer.parseInt(userID), userName, userSex, Integer.parseInt(age), studyID, token);
		
		JSONObject jsonResult;
		if (result==null||result.isEmpty())
		{
			jsonResult=null;
		}
		else
		{
			jsonResult=new JSONObject(result);
		}
		
		switch(caseName)
		{
			case "Success":
				String subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserIDWrong":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserNameWrong":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserNameNull":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserSexWrong":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserSexNull":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "AgeWrong":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "StudyIDWrong":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "StudyIDNull":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "TokenWrong":
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Assert.assertNull(result);
				break;
			default:
				break;
		}	
		
	}
	
	@Test(dataProvider="Test_NoReport",groups="Test")
	public void testStartChatTestCase_NoReport(String caseName,String userID,String userName,String userSex,String age,String headPicture,String relationShip,String studyID,String attachPics,String bodyContent,String token) throws NumberFormatException, JSONException, IOException
	{
		String[] picture=attachPics.split(";");
		//调用接口
		String result=StartChat.post(Integer.parseInt(userID), userName, userSex, Integer.parseInt(age), headPicture, relationShip, studyID, picture, bodyContent, token);
		
		JSONObject jsonResult;
		if (result==null||result.isEmpty())
		{
			jsonResult=null;
		}
		else
		{
			jsonResult=new JSONObject(result);
		}
		
		switch (caseName)
		{
			case "Success":
				String subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserIDWrong":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserNameWrong":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserNameNull":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserSexWrong":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "UserSexNull":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "AgeWrong":
				subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
				CloseSubject.get(subjectID, token);
				Assert.assertNotEquals(jsonResult.getJSONObject("Data").getString("SubjectID"), "0");
				break;
			case "TokenWrong":
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Assert.assertNull(result);
				break;
			default:
				break;
		}
		
	}
	/**
	 * 测试后下线医生
	 * @throws IOException
	 */
	@AfterTest(groups="Test")
	public void testStartChatAfterTest() throws IOException
	{
		Host.setup("Test");
		String doctorID=Config.getValue("Doctor-Test", "ID");
		String doctorToken=Config.getValue("Doctor-Test", "token");
		UpdateStatus.put("false", "1", doctorID, doctorToken);
	}
}
